package com.graph;

import java.util.*;

public class Solution797 {
    List<List<Integer>> res = new LinkedList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        path.add(0);
        dfs(graph, 0);
        return res;
    }

    public void dfs(int[][] graph, int n) {
        if (n == graph.length - 1) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < graph[n].length; i++) {
            path.add(graph[n][i]);
            dfs(graph, graph[n][i]);
            path.removeLast();
        }
    }
}
